﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using UDS.Entity;
using System.Web.Caching;

namespace UDS.Components
{
    /// <summary>
    /// 接点管理
    /// </summary>
    public class Class
    {
        private const string ClassCacheKey = HttpUtil.CachePrefix + "Class";

        /// <summary>
        /// Get all classes from database.
        /// </summary>
        public static IList<ClassInfo> GetClasses(bool allowCache)
        {
            ReadDataAction<IList<ClassInfo>> callback = delegate
            {
                using (var db = new Database())
                {
                    using (var dr = db.RunDataReader("dbo.sproc_Class_GetClasses_V2", null))
                    {
                        var list = new List<ClassInfo>();
                        while (dr.Read())
                        {
                            list.Add(ClassInfo.ReadClassInfo(dr));
                        }

                        return list.AsReadOnly();
                    }
                }
            };

            if (allowCache)
            {
                return HttpUtil.ReadCache(ClassCacheKey, callback, 5);
            }
            return callback();
        }

        public static ClassInfo GetClass(int classId)
        {
            return GetClass(classId, true);
        }

        public static ClassInfo GetClass(int classId, bool allowCache)
        {
            var list = GetClasses(allowCache);
            foreach (var item in list)
            {
                if (item.ID == classId)
                {
                    return item;
                }
            }

            return null;
        }

        /// <summary>
        /// 获取接点类型
        /// </summary>
        /// <param name="ClassID">接点ID</param>
        /// <returns>返回接点类型</returns>
        public static ClassType GetClassType(int classId)
        {
            ClassInfo cls = GetClass(classId, true);
            if (cls != null)
            {
                return cls.Type;
            }
            return ClassType.Unknown;
        }

        /// <summary>
        /// 获取接点名字
        /// </summary>
        /// <param name="ClassID">接点ID</param>
        /// <returns>返回接点名字</returns>
        public static string GetClassName(int classId)
        {
            ClassInfo cls = GetClass(classId, true);
            if (cls != null)
            {
                return cls.Name;
            }
            return string.Empty;
        }

        /// <summary>
        /// 加目录节点
        /// </summary>
        /// <param name="ParentID">加目录节点</param>
        /// <returns>加目录节点</returns>
#pragma warning disable 1573
        public void AddClass(int ParentID, string ClassName, string ClassRemark, int ClassType, string AddedBy, DateTime AddedDate, int Status)
#pragma warning restore 1573
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@ClassParentID", SqlDbType.Int, 20, ParentID),
                Database.MakeInParam("@ClassName", SqlDbType.NVarChar, 20, ClassName),
                Database.MakeInParam("@ClassRemark", SqlDbType.NVarChar, 400, ClassRemark),
                Database.MakeInParam("@ClassType", SqlDbType.Int, 1, ClassType),
                Database.MakeInParam("@AddedBy", SqlDbType.NVarChar, 40, AddedBy),
                Database.MakeInParam("@AddedDate", SqlDbType.DateTime, 30, AddedDate),
                Database.MakeInParam("@Status", SqlDbType.Int, 1, Status)
            };

            try
            {
                data.RunProc("dbo.sproc_AddTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("目录添加出错", ex);
            }
        }

#if false
        #region 获取用户相关项目

        /// <summary>
        /// 获取用户相关项目
        /// </summary>
        public SqlDataReader GetDeliverClass(string Username)
        {
            // create data object and params
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 20, Username)
            };

            try
            {
                // run the stored procedure
                return data.RunDataReader("dbo.sproc_GetDeliverClass", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获取用户相关项目出错!", ex);
            }
        }

        #endregion
#endif
    }
}